package com.inspinia.upms.rpc.service;

import com.inspinia.base.dao.BaseDao;
import com.inspinia.base.service.BaseServiceImpl;
import com.inspinia.upms.common.model.resource.UpmsResource;
import com.inspinia.upms.dao.UpmsResourcesDao;
import com.inspinia.upms.rpc.api.UpmsResourceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.ValidationException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author veblen
 * @date 16-2-1
 */
@Service
public class UpmsResourcesServiceImpl extends BaseServiceImpl<UpmsResource> implements UpmsResourceService {
    @Resource
    UpmsResourcesDao upmsResourcesDao;

    @Override
    protected BaseDao<UpmsResource> getDao() {
        return upmsResourcesDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(UpmsResource bean) {
        validate(bean);
        if (bean.getPid() == null) {
            bean.setPid(UpmsResource.ROOT_PID);
            bean.setLvl(1);
        } else {
            UpmsResource parent = get(bean.getPid());
            if (parent != null) {
                bean.setLvl(parent.getLvl() + 1);
            }
        }
        if (bean.getSeq() == null) {
            Integer maxSeq = upmsResourcesDao.findMaxSeq(bean.getPid());
            bean.setSeq(maxSeq != null ? maxSeq + 1 : 1);
        }
        return super.insert(bean);
    }

    private void validate(UpmsResource bean) {
        boolean b = validateName(bean.getName(), bean.getId(), bean.getPid());
        if (b) {
            try {
                throw new ValidationException("名称已存在");
            } catch (ValidationException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Set<Long> getDistResources(Set<Long> resources) {
        Set<Long> pidResources = new HashSet<>();
        //系统 默认将顶级菜单选中
        resources.add(1L);
        for (Long id : resources) {
            //将菜单的父级菜单添加到 选中菜单中
            UpmsResource resource = get(id);
            if (resource != null && resource.getPid() != null) {
                pidResources.add(resource.getPid());
            }
        }
        resources.addAll(pidResources);
        return resources;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpmsResource bean) {
        validate(bean);
        if (bean.getSeq() == null) {
            int maxSeq = upmsResourcesDao.findMaxSeq(bean.getPid());
            bean.setSeq(maxSeq + 1);
        }
        return super.update(bean);
    }

    @Override
    public List<Long> findResourceIdsByPostIds(List<Long> postIds) {
        return upmsResourcesDao.findResourceIdsByPostIds(postIds);
    }

    @Override
    public List<UpmsResource> findAll() {
        return upmsResourcesDao.findAll();
    }

    @Override
    public boolean validateName(String resourceName, Long resourceId, Long pid) {
        return upmsResourcesDao.countByName(resourceName, resourceId, pid) > 0;
    }

    @Override
    public Set<Long> getDistUpmsResource(Set<Long> UpmsResource) {
        Set<Long> pidUpmsResource = new HashSet<>();
        //系统 默认将顶级菜单选中
        UpmsResource.add(1L);
        for (Long id : UpmsResource) {
            //将菜单的父级菜单添加到 选中菜单中
            UpmsResource resource = get(id);
            if (resource != null && resource.getPid() != null) {
                pidUpmsResource.add(resource.getPid());
            }
        }
        UpmsResource.addAll(pidUpmsResource);
        return UpmsResource;

    }

    @Override
    public List<UpmsResource> findMenusByRole(List<Long> roleIds) {

        return upmsResourcesDao.findMenusByRole(roleIds);
    }

    @Override
    public List<Long> findResourceIdsByRoleIds(List<Long> roleIds) {
        return upmsResourcesDao.findResourceIdsByRoleIds(roleIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStatus(UpmsResource bean) {
        int n = 0;
        ids.clear();
        findChilderId(bean.getId());
        for (Long id : ids) {
            UpmsResource resource = upmsResourcesDao.findById(id);
            resource.setStatus(bean.getStatus());
            n = upmsResourcesDao.updateStatus(resource);
        }
        return n;
    }

    List<Long> ids = new ArrayList<>();

    public void findChilderId(Long id) {
        List<Long> list = upmsResourcesDao.findChildrenUpmsResource(id);
        ids.add(id);
        if (list.size() != 0) {
            for (int i = 0; i < list.size(); i++) {
                this.findChilderId(list.get(i));
            }
        }

    }

    @Override
    public int delete(Long id) {
        int n = 0;
        ids.clear();
        findChilderId(id);
        for (Long resourceId : ids) {
            n = upmsResourcesDao.delete(resourceId);
        }
        return n;
    }

}
